import java.util.PriorityQueue;

public class Solution1499 {
    public int findMaxValueOfEquation(int[][] points, int k) {
        PriorityQueue<int[]> queue=new PriorityQueue<>((a,b)->((b[1]-b[0]))-(a[1]-a[0]));
        int ans=Integer.MIN_VALUE;
        for (int[] point : points) {
            while (!queue.isEmpty() && point[0] - queue.peek()[0] > k) {
                queue.poll();
            }
            if (!queue.isEmpty()) {
                ans = Math.max(ans, queue.peek()[1] + point[1] + point[0] - queue.peek()[0]);
            }
            queue.offer(point);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1499().findMaxValueOfEquation(new int[][]{{1,3},{2,0},{5,10},{6,-10}},1));
    }
}
